﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0002", Name = "用户注册")]
    public class URegister : IProcessMessage
    {
        private BLLULogin bllULogin;
        public BLLULogin BllUlogin
        {
            get
            {
                if (bllULogin == null)
                    bllULogin = BLLFactory.getInstance().CreateULoginBLL();
                return bllULogin;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                EUser user = new EUser();
                user.UserName = reqMsg.Body.GetFieldValue("UserName");
                user.Password = reqMsg.Body.GetFieldValue("Password");
                user.Mobile = reqMsg.Body.GetFieldValue("Mobile");
                user.Email = reqMsg.Body.GetFieldValue("EMail");
                user.UserId = Guid.NewGuid();
                user.Disabled = 0;
                user.UserType = 1;
                if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Password) || string.IsNullOrEmpty(user.Mobile))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                if (BllUlogin.IsUserExit(user.UserName))
                {
                    rspMsg.Head.RspCode = MessageCode.Login_Faild_Already_Exist.Code;
                    rspMsg.Head.RspMsg = MessageCode.Login_Faild_Already_Exist.Msg;

                    return rspMsg;
                }
                else
                {
                    if (BllUlogin.InsertNewUser(user))
                    {
                        rspMsg.Head.RspCode = MessageCode.Success.Code;
                        rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                        return rspMsg;
                    }
                    else
                    {
                        rspMsg.Head.RspCode = MessageCode.Server_Error.Code;
                        rspMsg.Head.RspMsg = MessageCode.Server_Error.Msg;

                        return rspMsg;
                    }
                }
            }
            catch (Exception e)
            {

                ErrorLogWriter.Write("UGetCommodityListByShopId-" + e);
                throw e;
            }
        #endregion
        }
    }
}